/*
 * RoleController.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the web-geek, Version 1.0 (the "License");
 *
 */
package org.geek.role.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.geek.common.base.BaseController;
import org.geek.common.entity.ColumnModel;
import org.geek.common.entity.Criteria;
import org.geek.common.entity.ExtGridReturn;
import org.geek.common.entity.ExtPager;
import org.geek.common.entity.ExtReturn;
import org.geek.domain.Role;
import org.geek.role.service.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
@Controller
@RequestMapping("/role/")
public class RoleController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@Resource
	private IRoleService roleService;
	
	@RequestMapping(value="initPage.jhtml",method=RequestMethod.GET)
	@Override
	public String initPage(HttpServletRequest request, ModelMap model) {
		List<ColumnModel> list = new ArrayList<ColumnModel>();
		list.add(new ColumnModel("角色Id","id",true,true));
		list.add(new ColumnModel("角色名","roleName",false,false));
		list.add(new ColumnModel("角色描述","roleDesc",false,false));
		
		model.put("columnModel", list);
		
		model.put("parentWinId", request.getParameter("parentWinId"));
		model.put("winId", request.getParameter("winId"));
		
		log(request,logger,"访问角色管理");
		return "page/role";
	}
	
	/**
	 * @return 
	 */
	@RequestMapping(value="getDataList.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object getDataList(ExtPager pager){
		Criteria criteria = new Criteria();
		int total = roleService.countRoleByCriteria(criteria);
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setEnd(pager.getLimit() + pager.getStart());
			criteria.setStart(pager.getStart());
		}
		List<Role> list = roleService.getRoleByCriteria(criteria);
		return new ExtGridReturn(total, list);
	}
	
	@RequestMapping(value="addRole.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object addRole(Role role){
		boolean success = true;
		
		if(StringUtils.isEmpty(role.getRoleName()))success = false;
		
		if(success && roleService.addRole(role)<=0){
			success = false;
		}
		String msg = success?"数据保存成功！":"数据保存失败！";
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="modRole.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object modRole(Role role){
		boolean success = true;
		
		if(StringUtils.isEmpty(role.getRoleName()))success = false;
		
		if(success && roleService.modRole(role)<=0){
			success = false;
		}
		String msg = success?"数据保存成功！":"数据保存失败！";
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="delRoleByRoleId.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object delRoleByRoleId(int id){
		boolean success = true;
		if(roleService.delRoleByRoleId(id)<=0){
			success = false;
		}
		String msg = success?"数据删除成功！":"数据删除失败！";
		return new ExtReturn(success, msg);
	}

}
